<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%email_record}}`.
 */
class m230917_075916_create_email_record_table extends Migration
{
    const TABLE_NAME = '{{%email_record}}';

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=MyISAM COMMENT="邮件记录表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey()->comment('ID'),
            'category' => $this->string()->notNull()->comment('邮件种类'),
            'content' => 'mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT "邮件内容"',
            'code' => $this->string(10)->notNull()->defaultValue('')->comment('验证码'),
            'sender' => $this->integer()->notNull()->defaultValue(0)->comment('发送人'),
            'receive_email' => $this->string()->notNull()->comment('接收邮箱'),
            'created_at' => $this->dateTime()->notNull(),
        ], $tableOptions);

        $this->createIndex('idx_sender', self::TABLE_NAME, 'sender');
        $this->createIndex('idx_created_at', self::TABLE_NAME, 'created_at');
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
